package com.xiaotao.publisher;

import com.xiaotao.config.RabbitMessageUtil;
import com.xiaotao.util.ConstantUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class OrderDelayPublisher {
    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private RabbitMessageUtil rabbitMessageUtil;

    //发送延迟消息，过期取消订单
    public void send(String msg,Integer timeout){
        rabbitTemplate.setConfirmCallback(rabbitMessageUtil);
        rabbitTemplate.setReturnsCallback(rabbitMessageUtil);
        rabbitTemplate.convertAndSend(ConstantUtil.EXCHANGE_DELAY_CHECK_ORDER, ConstantUtil.ROUTEING_KEY_DELAY_ORDER, msg, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(timeout);
                return message;
            }
        });
    }
}
